﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Seta;
using System.Windows;
using System.Collections;
using System.Windows.Media;

namespace Árvore_Binária_de_Pesquisa
{
    public class ControlVariables : Controls
    {                
        /// <summary>
        /// Keeps the original margins of the elements
        /// </summary>
        private static ArrayList margins = new ArrayList();


        #region Tree Operations
        internal void showCompleteTree()
        {
            myTree.showCompleteTree(elemLabels, myArrows);
        }

        internal void createEmptyTree()
        {
            myTree.createEmptyTree(elemLabels, myArrows);
        }

        internal void destroyTree()
        {
            myTree.destroyTree(elemLabels, myArrows);
        }

        internal void insertTreeNode()
        {
            myTree.insertTreeNode(txtInput, txtTrace, elemLabels, myArrows);
        }

        internal void startSearchTreeNodeAnimation()
        {
            myTree.startSearchTreeNodeAnimation(txtInput, txtTrace, this);
        }

        internal void startRemoveTreeNodeAnimation()
        {
            myTree.startRemoveTreeNodeAnimation(txtInput, txtTrace, this);
        }

        internal void removeTreeNode(string nodeToRemove)
        {
            myTree.removeTreeNode(nodeToRemove, Menus.removePredecessor);
        }

        internal bool treeIsEmpty()
        {
            return myTree.isTreeEmpty();
        }

        internal void drawTree()
        {
            myTree.drawTree(elemLabels, myArrows);
        }
        
        internal bool foundNodeToRemove()
        {
            return myTree.foundNodeToRemove;
        }

        internal int getNodePosition(string p)
        {
            return myTree.getNodePosition(p);
        }
        #endregion

        #region txtInput Operations
        internal string getInputText()
        {
            return txtInput.Text;
        }

        internal void fillEntrada(string p)
        {
            txtInput.Text = "";
        }        
        #endregion

        #region Buttons and Menus Operations
        internal void disableButtonsAndMenus()
        {
            Buttons.desabilitaBotoes(btns);
            Menus.disableMenuItems(MenuItems);
        }

        internal void enableMenuItems()
        {
            Menus.enableMenuItems(MenuItems);
        }

        internal void updateButton(bool all, bool createAndComplete)
        {
            Buttons.updateButtons(btns, all, createAndComplete);
        }
        #endregion

        #region Labels Operations
        internal Label getLabel(int p)
        {
            return elemLabels.getLabel(p);
        }

        internal ArrayList getLabel()
        {
            return elemLabels.getLabel();
        }

        internal void setLabelVisibility(int p, Visibility visibility)
        {
            elemLabels.setVisibility(p, visibility);
        } 
        #endregion        

        #region Arrows Operations
        internal ArrowLine getArrow(int ind)
        {
            return myArrows.getArrow(ind);
        }

        internal ArrowLine getColoredArrow(int ind)
        {
            return myArrows.getColoredArrow(ind);
        }

        internal void clearArrowsToAnimate()
        {
            myArrows.clearArrowsToAnimate();
        }

        internal Hashtable getArrows()
        {
            return myArrows.getArrows();
        }

        internal void addArrowToAnimate(int p)
        {
            myArrows.addArrowToAnimate(p);
        }

        internal ArrayList getArrowsToAnimate()
        {
            return myArrows.getArrowsToAnimate();
        }

        internal double getArrowPosition(ArrowCoordinate ac, int p)
        {
            return myArrows.getArrowPosition(ac, p);
        }

        internal void setArrowVisibility(int p, Visibility visibility)
        {
            myArrows.setVisibility(p, visibility);
        } 
        #endregion
        
        #region Animations Operations
        /// <summary>
        /// Calls the functions to animate the search and remove operations
        /// </summary>
        /// <param name="isRemoveNode">
        /// Indicates if it is a remove operation
        /// </param>
        /// <param name="posicoes">
        /// The positions that form the animations course
        /// </param>
        internal void animateSearchAndRemove(bool isRemoveNode, Array positions)
        {
            if (isRemoveNode)
            {
                myAnimations.nodeToRemove = txtInput.Text;
            }

            myAnimations.isRemoveOperation = isRemoveNode;
            myAnimations.defineAnimation(positions, false);

            txtInput.Text = "";
        }

        internal void animateTraversal(Array positions, Array addToCaminho)
        {
            myAnimations.defineAnimacaoCaminhamento(positions, addToCaminho);
        }

        internal void updateSenteceAndColor(string sentence, Brush color)
        {
            myAnimations.sentence = sentence;
            myAnimations.color = color;
        } 
        #endregion      

        /// <summary>
        /// Returns the original margins of the element indicated by index "ind"
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        internal Thickness getOriginalMargins(int ind)
        {
            return ((Thickness)margins[ind]);
        }

        /// <summary>
        /// Saves the original margins informations and calls the function that calculates
        /// the arrows size and positions
        /// </summary>
        internal void initiateMargins()
        {
            foreach (Label lbl in elemLabels.getLabel())
            {
                margins.Add(lbl.Margin);
            }
            
            myArrows.calculateArrowsMargins(elemLabels);            
        }

        /// <summary>
        /// Set all elements and arrows hidden to represent an empty tree
        /// </summary>
        internal void setAllHidden()
        {
            myArrows.setAllHidden();
            elemLabels.setAllHidden();
        }

    }
}
